package org.gd.hellojavacore.serial;

import java.io.*;

/**
 * @author Kononenko Philipp
 * @author Gorhover
 */
public class SerializationTest {

    public static void main(String... args) throws  Exception {
        String path = System.getProperty("user.home") + File.separator + "test" + File.separator + "ser.s";
        DataOutput out = new ObjectOutputStream(new FileOutputStream(path));
        SString ss = new SString();
        SDouble sd = new SDouble();
        test(ss, out);
        test(sd, out);
    }
    public static void test(Test t, DataOutput out) throws Exception {
        long start = System.currentTimeMillis();
        System.out.println("Starting ");
        for(int i = 0; i < 1000; i++ ) {
            t.perform(out);
        }
        System.out.println(t.getClass().getSimpleName() + ": Done. Time took " + (System.currentTimeMillis() -  start));
    }
}

interface Test extends Serializable {

    void perform(DataOutput out) throws Exception;
}

class SString implements Test {

    private double value = 25.44444444441;

    @Override
    public void perform(DataOutput out) throws IOException {
        out.writeUTF(String.format("%.2f", value));
    }

}
class SDouble implements Test {

    private double value;

    SDouble() {
        value=25.44444444441;
    }

    @Override
    public void perform(DataOutput out) throws IOException {
        out.writeDouble(value);
    }
}
